package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.HmzEmployeeInfo;
import com.ruoyi.system.mapper.HmzEmployeeInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HmzMoneyInfoMapper;
import com.ruoyi.system.domain.HmzMoneyInfo;
import com.ruoyi.system.service.IHmzMoneyInfoService;

import static com.ruoyi.common.utils.PageUtils.clearPage;

/**
 * 金额记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-19
 */
@Service
public class HmzMoneyInfoServiceImpl implements IHmzMoneyInfoService
{
    @Autowired
    private HmzMoneyInfoMapper hmzMoneyInfoMapper;
    @Autowired
    private HmzEmployeeInfoMapper hmzEmployeeInfoMapper;

    /**
     * 查询金额记录
     *
     * @param id 金额记录主键
     * @return 金额记录
     */
    @Override
    public HmzMoneyInfo selectHmzMoneyInfoById(Long id)
    {
        return hmzMoneyInfoMapper.selectHmzMoneyInfoById(id);
    }

    /**
     * 查询金额记录列表
     *
     * @param hmzMoneyInfo 金额记录
     * @return 金额记录
     */
    @Override
    public List<HmzMoneyInfo> selectHmzMoneyInfoList(HmzMoneyInfo hmzMoneyInfo)
    {

        String employeeName = hmzMoneyInfo.getEmployeeName();
        String employeeRfidCode = hmzMoneyInfo.getEmployeeRfidCode();
        if(employeeName !=null && !"".equals(employeeName)){
            HmzEmployeeInfo hmzEmployeeInfo =new HmzEmployeeInfo();
            hmzEmployeeInfo.setName(employeeName);
            List<HmzEmployeeInfo> hmzEmployeeInfos = hmzEmployeeInfoMapper.selectHmzEmployeeInfoList(hmzEmployeeInfo);
            if(hmzEmployeeInfos !=null && !hmzEmployeeInfos.isEmpty()){
                hmzMoneyInfo.setEmployeeId(hmzEmployeeInfos.get(0).getId());
            }else {
                hmzMoneyInfo.setEmployeeId(0L);
            }
        }
        if(employeeRfidCode !=null && !"".equals(employeeRfidCode)){
            HmzEmployeeInfo hmzEmployeeInfo =new HmzEmployeeInfo();
            hmzEmployeeInfo.setName(employeeRfidCode);
            List<HmzEmployeeInfo> hmzEmployeeInfos = hmzEmployeeInfoMapper.selectHmzEmployeeInfoList(hmzEmployeeInfo);
            if(hmzEmployeeInfos !=null && !hmzEmployeeInfos.isEmpty()){
                hmzMoneyInfo.setEmployeeId(hmzEmployeeInfos.get(0).getId());
            }else {
                hmzMoneyInfo.setEmployeeId(0L);
            }
        }

//        构造人员信息
        List<HmzMoneyInfo> hmzMoneyInfos = hmzMoneyInfoMapper.selectHmzMoneyInfoList(hmzMoneyInfo);
        HmzMoneyInfo hmzMoneyInfo1 = new HmzMoneyInfo();
        if((employeeName !=null && !employeeName.isEmpty()) || (employeeRfidCode !=null && !employeeRfidCode.isEmpty()) ){
            hmzMoneyInfo1.setEmployeeId(hmzMoneyInfo.getEmployeeId());
        }
        hmzMoneyInfo1.setDay(hmzMoneyInfo.getDay());
        hmzMoneyInfo1.setMonth(hmzMoneyInfo.getMonth());
        BigDecimal reduce = hmzMoneyInfoMapper.selectHmzMoneyInfoListMoneySum(hmzMoneyInfo1);
//        BigDecimal reduce = hmzMoneyInfos1 == null? BigDecimal.valueOf(0) :hmzMoneyInfos.stream().map(HmzMoneyInfo::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        if (hmzMoneyInfos != null) {
            for(HmzMoneyInfo item : hmzMoneyInfos){
                //根据人员id查询信息
                HmzEmployeeInfo hmzEmployeeInfo = hmzEmployeeInfoMapper.selectHmzEmployeeInfoById(item.getEmployeeId());
                if(hmzEmployeeInfo !=null){
                    Date createTime = item.getCreateTime();
                    // 定义日期格式
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    // 将 Date 对象格式化为指定格式的字符串
                    String formattedDate = sdf.format(createTime);
                    item.setCreateTimes(formattedDate);
                    item.setEmployeeName(hmzEmployeeInfo.getName());
                    item.setEmployeeRfidCode(hmzEmployeeInfo.getRfidCode());
                    item.setMoneys(reduce);
                }

            }
        }
        return hmzMoneyInfos;
    }

    /**
     * 新增金额记录
     *
     * @param hmzMoneyInfo 金额记录
     * @return 结果
     */
    @Override
    public int insertHmzMoneyInfo(HmzMoneyInfo hmzMoneyInfo)
    {
        hmzMoneyInfo.setCreateTime(DateUtils.getNowDate());
        return hmzMoneyInfoMapper.insertHmzMoneyInfo(hmzMoneyInfo);
    }

    /**
     * 修改金额记录
     *
     * @param hmzMoneyInfo 金额记录
     * @return 结果
     */
    @Override
    public int updateHmzMoneyInfo(HmzMoneyInfo hmzMoneyInfo)
    {
        hmzMoneyInfo.setUpdateTime(DateUtils.getNowDate());
        return hmzMoneyInfoMapper.updateHmzMoneyInfo(hmzMoneyInfo);
    }

    /**
     * 批量删除金额记录
     *
     * @param ids 需要删除的金额记录主键
     * @return 结果
     */
    @Override
    public int deleteHmzMoneyInfoByIds(Long[] ids)
    {
        return hmzMoneyInfoMapper.deleteHmzMoneyInfoByIds(ids);
    }

    /**
     * 删除金额记录信息
     *
     * @param id 金额记录主键
     * @return 结果
     */
    @Override
    public int deleteHmzMoneyInfoById(Long id)
    {
        return hmzMoneyInfoMapper.deleteHmzMoneyInfoById(id);
    }

    @Override
    public JSONObject  listMonety() {
        JSONObject jsonObject =new JSONObject();
        List<HmzMoneyInfo> hmzMoneyInfos = hmzMoneyInfoMapper.listNoEmployee();
        jsonObject.put("listNoEmployee",hmzMoneyInfos);
        List<HmzMoneyInfo> hmzMoneyInfos1 = hmzMoneyInfoMapper.listEmployee();
        jsonObject.put("hmzMoneyInfos1",hmzMoneyInfos1);
        //每个月充值的金额
        List<HmzMoneyInfo> hmzMoneyInfos2 = hmzMoneyInfoMapper.listEmployee1();
        jsonObject.put("hmzMoneyInfos2",hmzMoneyInfos2);
        return jsonObject;


    }
}
